package com.meis.widget.horizontal;

import com.meis.widget.utils.DensityUtil;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

/**
 * Created by wenshi on 2018/12/12.
 * Description 豆瓣弹性滑动控件
 */
public class HorizontalScrollView extends DependentLayout implements Component.TouchEventListener, ComponentContainer.ArrangeListener {

    private static final String SCROLL_MORE = "左滑看更多";
    private static final String RELEASE_MORE = "松手看更多";
    private static final float RATIO = 0.4f;
    private ListContainer mHorizontalRecyclerView;
    private VerticalTextView mMoreTextView;
    private boolean mShowMore = true;
    private float mHintLeftMargin = 0;
    private int mOffsetWidth = 0;
    private float mLastX;
//    private float mLastY;
//    private boolean mConsumeMoveEvent = false;
    private int mMoveIndex = 0;
    // 回弹动画
    private AnimatorValue ReboundAnim;
    private OnReleaseListener mListener;

    public HorizontalScrollView(Context context) {
        this(context, null);
    }

    public HorizontalScrollView(Context context, AttrSet attrs) {
        this(context, attrs, "");
    }

    public HorizontalScrollView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mOffsetWidth = -DensityUtil.dp2px(context, 70);

        setTouchEventListener(this);
        setArrangeListener(this);
    }

    @Override
    public boolean onArrange(int i, int i1, int i2, int i3) {
        if (mMoreTextView != null) {
            mOffsetWidth = -mMoreTextView.getWidth();
            mOffsetWidth = mOffsetWidth == 0 ? -DensityUtil.dp2px(getContext(), 70) : mOffsetWidth;
        }
        return false;
    }

    @Override
    public void addComponent(Component child, ComponentContainer.LayoutConfig params) {
        child.setLayoutConfig(params);
        addComponent(child);
    }

    @Override
    public void addComponent(Component child) {
        if (child instanceof ListContainer) {
            mHorizontalRecyclerView = (ListContainer) child;
        } else if (child instanceof VerticalTextView) {
            mMoreTextView = (VerticalTextView) child;
        }
        super.addComponent(child);
    }

    private ComponentParent getParentListView(ComponentParent viewParent) {
        if (viewParent == null) return null;
        if (viewParent instanceof ListContainer) {
            return viewParent;
        } else {
            getParentListView(viewParent.getComponentParent());
        }
        return null;
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent ev) {
        if (mHorizontalRecyclerView == null) {
            return false;
        }
        switch (ev.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                mHintLeftMargin = 0;
                mMoveIndex = 0;
//                mConsumeMoveEvent = false;
                MmiPoint point = ev.getPointerPosition(ev.getIndex());
                mLastX = point.getX();
//                mLastY = point.getY();
                break;

            case TouchEvent.POINT_MOVE:
                // 释放动画
                if (ReboundAnim != null && ReboundAnim.isRunning()) {
                    break;
                }
                MmiPoint mmiPoint = ev.getPointerPosition(ev.getIndex());
                float mDeltaX = (mmiPoint.getX() - mLastX);
                //不需要处理事件冲突，否则会导致其他问题
//                float mDeltaY = mmiPoint.getY() - mLastY;
//                if (!mConsumeMoveEvent) {
//                    // 处理事件冲突
//                    if (Math.abs(mDeltaX) > Math.abs(mDeltaY)) {
//                        ((Component) getComponentParent()).setEnabled(false);
//                    } else {
//                        ((Component) getComponentParent()).setEnabled(true);
//                    }
//                }

                mMoveIndex++;

                if (mMoveIndex > 2) {
//                    mConsumeMoveEvent = true;
                }

                MmiPoint pointer = ev.getPointerPosition(ev.getIndex());
                mLastX = pointer.getX();
//                mLastY = pointer.getY();
                mDeltaX = mDeltaX * RATIO;

                // 右滑
                if (mDeltaX > 0) {
                    if (!mHorizontalRecyclerView.canScroll(DRAG_RIGHT) || mHorizontalRecyclerView.getTranslationX() < 0) {
                        float transX = mDeltaX + mHorizontalRecyclerView.getTranslationX();
                        if (mHorizontalRecyclerView.canScroll(DRAG_RIGHT) && transX >= 0) {
                            transX = 0;
                        }
                        mHorizontalRecyclerView.setTranslationX(transX);
                        setHintTextTranslationX(mDeltaX);
                    }
                } else if (mDeltaX < 0) { // 左滑
                    if (!mHorizontalRecyclerView.canScroll(DRAG_LEFT) || mHorizontalRecyclerView.getTranslationX() > 0) {
                        float transX = mDeltaX + mHorizontalRecyclerView.getTranslationX();
                        if (transX <= 0 && mHorizontalRecyclerView.canScroll(DRAG_LEFT)) {
                            transX = 0;
                        }
                        mHorizontalRecyclerView.setTranslationX(transX);
                        setHintTextTranslationX(mDeltaX);
                    }
                }
                break;

            case TouchEvent.CANCEL:
            case TouchEvent.PRIMARY_POINT_UP:
//                ((Component) getComponentParent()).setEnabled(true);
                // 释放动画
                if (ReboundAnim != null && ReboundAnim.isRunning()) {
                    break;
                }

                if (mOffsetWidth != 0 && mHintLeftMargin <= mOffsetWidth && mListener != null) {
                    mListener.onRelease();
                }
                ReboundAnim = new AnimatorValue();
                ReboundAnim.setDuration(300);
                ReboundAnim.setValueUpdateListener((animatorValue, v) -> {
                    float value = 1 - v;
                    mHorizontalRecyclerView.setTranslationX(value * mHorizontalRecyclerView.getTranslationX());
                    mMoreTextView.setTranslationX(value * mMoreTextView.getTranslationX());
                });
                ReboundAnim.start();
                break;
        }
        return true;
    }

    private void setHintTextTranslationX(float deltaX) {
        if (mShowMore) {
            float offsetX = 0;
            if (mMoreTextView != null) {
                mHintLeftMargin += deltaX;
                if (mHintLeftMargin <= mOffsetWidth) {
                    offsetX = mOffsetWidth;
                    mMoreTextView.setVerticalText(RELEASE_MORE);
                } else {
                    offsetX = mHintLeftMargin;
                    mMoreTextView.setVerticalText(SCROLL_MORE);
                }
                mMoreTextView.setOffset(offsetX, mOffsetWidth);
                mMoreTextView.setTranslationX(offsetX);
            }
        }
    }

    public boolean getShowMore() {
        return mShowMore;
    }

    public void setShowMore(boolean showMore) {
        mShowMore = showMore;
    }

    public void setOnReleaseListener(OnReleaseListener listener) {
        this.mListener = listener;
    }

    public interface OnReleaseListener {
        void onRelease();
    }
}
